#include <flipc/gl/window.h>
#include <flipc/gl/texture.h>
#include <flipc/raster/image.h>
#include <flipc/raster/color.h>
#include <flipc/gl/buttons.h>
#include <flipc/gl/gl.h>

#include <math.h>

int color = 0;

FLIP_BUTTON_FUN(on_button, id, state)
{
}


int test()
{
	flip_window* w = flip_create_window();
	flip_image_data img;
	flip_texture tex;
	uint size = 256;
	
	double square[] =
	{
		0.0,0.0,
		size,0.0,
		size,size,
		0.0,size
	};
	
	double texcoords[] =
	{
		0.0, 0.0,
		1.0, 0.0,
		1.0, 1.0,
		0.0, 1.0
	};
	
	FLIP_EVENTS(w);
		FLIP_ON_BUTTON(on_button);
	FLIP_EVENTS_END();
	
	flip_window_set_mode(w, size, size, 0);
	flip_window_set_visible(w, 1);
	
	flip_init_image(&img, size, size, flip_pf_8880);
	tex = flip_create_texture(size, size);

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	
	while(0 == flip_window_update(w))
	{
	
		flip_texture_upload_subimage(tex, 0, 0,
			flip_image_width(&img),
			flip_image_height(&img),
			flip_image_pixels(&img));

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		switch(color)
		{
			case 0: glColor3d(1.0, 1.0, 1.0); break;
			case 1: glColor3d(0.5, 1.0, 0.0); break;
			case 2: glColor3d(0.0, 0.5, 1.0); break;
		}

		glBindTexture(GL_TEXTURE_2D, tex.id);
		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
			glVertexPointer(2, GL_DOUBLE, 0, square);
			glTexCoordPointer(2, GL_DOUBLE, 0, texcoords);
			glDrawArrays(GL_QUADS, 0, 4);
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);

		flip_window_throttle_fps(w, 50);
		flip_window_end_drawing(w);
	}
	
	flip_destroy_window(w);
	return 0;
}

